package com.kb.crawl;

import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;

import com.car.util.DocUtil;
import com.car.util.Util;
import com.kb.bean.KBOfPCAuto;
import com.kb.dao.KBDaoOfPCAuto;

public class KBCrawlOfPCAuto implements Runnable {
	private static Logger logger = Logger.getLogger(KBCrawlOfPCAuto.class.getName());
	private int kbid;
	private KBDaoOfPCAuto kbDaoOfPCAuto;
	public KBCrawlOfPCAuto(int kbid, KBDaoOfPCAuto kbDaoOfPCAuto) {
		super();
		this.kbid = kbid;
		this.kbDaoOfPCAuto = kbDaoOfPCAuto;
	}

	public void run() {
		this.startCrawl(this.kbid);
	}

	// 得到口碑的页数
	public int getPages(int kbid) {
		int page = 1;
		String url = "http://price.pcauto.com.cn/comment/sg" + kbid + "/";
		try {
			Document doc = DocUtil.getDoc(url);
			Elements pageList = doc.select("div.pcauto_page a");
			int size = pageList.size();
			Element maxPage = pageList.get(size - 2);
			page = Integer.parseInt(maxPage.text());
		} catch (Exception e) {
		}
		return page;
	}

	public void startCrawl(int kbid) {
		int page = getPages(kbid);
		for (int i = 1; i < page + 1; i++) {
			String url = "http://price.pcauto.com.cn/comment/sg" + kbid + "/p" + i + ".html";
			logger.info(url);
			Document doc = DocUtil.getDoc(url);
			Elements list = doc.select("div.main_body div.main_table");
			for (Element car : list) {
				Element left = car.select("div.main_table_left").get(0);
				Element right = car.select("div.main_table_right").get(0);
				Element user = left.select("div.user").get(0);
				Element userInfo = user.select("div.info a").get(0);
				int kb_yhid = Integer.parseInt(userInfo.attr("href").split("/")[3]);
				String kb_yhmc = userInfo.text();
				Element date = user.select("div.info a").get(1);
				String kb_fbrq = date.text().split("发表")[0];
				int kb_id = Integer.parseInt(date.attr("href").split("_")[1].split("\\.")[0]);
				int kb_sfjh = car.select("div.jh").size() > 0 ? 1 : 0;
				int kb_sftj = car.select("div.tj").size() > 0 ? 1 : 0;
				int kb_ppid = 0;
				int kb_cxid = 0;
				String kb_cxmc = "";
				String kb_gmsj = "";
				String kb_gmdd = "";
				double kb_lcj = 0;
				double kb_pjyh = 0;
				int kb_xslc = 0;
				Elements tr = car.select("div.car div.tr");
				for (Element t : tr) {
					String key = t.select("span.td1").text();
					switch (key) {
					case "购买车型":
						Element gmcx = t.select("span.td2 a").get(0);
						kb_ppid = Integer.parseInt(gmcx.attr("href").split("sg")[1].split("/m")[0]);
						kb_cxid = Integer.parseInt(gmcx.attr("href").split("/m")[1].split("/")[0]);
						kb_cxmc = gmcx.text();
						break;
					case "购买时间":
						Element gmsj = t.select("span.td2").get(0);
						kb_gmsj = gmsj.text().replaceAll("年", "-").replaceAll("月", "-") + "01";
						break;
					case "购买地点":
						kb_gmdd = t.select("span.td2").get(0).text();
						break;
					case "裸车价格":
						Element lcj = t.select("span.td2 em.red").get(0);
						kb_lcj = Double.parseDouble(lcj.text());
						break;
					case "平均油耗":
						Element pjhy = t.select("span.td2 em.red").get(0);
						kb_pjyh = Double.parseDouble(pjhy.text());
						break;
					case "行驶里程":
						Element xslc = t.select("span.td2").get(0);
						kb_xslc = Integer.parseInt(xslc.text().split("公里")[0]);
						break;

					default:
						break;
					}
				}
				Elements nr = right.select("div.table_text div.section");
				String kb_nr = "";
				for (Element e : nr) {
					String le = e.select("strong.title").text();
					Element p = e.select("p.text").get(0);
					for (TextNode text : p.textNodes()) {
						if (text.text().trim().length() > 0) {
							kb_nr = kb_nr + "\n" + le + (text.text().trim());
						}
					}
				}
				double kb_zhpf;
				int kb_wg = 0;
				int kb_ns = 0;
				int kb_kj = 0;
				int kb_pz = 0;
				int kb_dl = 0;
				int kb_ck = 0;
				int kb_yh = 0;
				int kb_ss = 0;
				int kb_hfs = 0;
				int kb_djs = 0;
				Elements pingfen = car.select("div.car div.scoreAreaTr");
				for (Element score : pingfen) {
					String span = score.select("span").text();
					int f = Integer.parseInt(score.select("div.board").text());
					switch (span) {
					case "外观":
						kb_wg = f;
						break;
					case "内饰":
						kb_ns = f;
						break;
					case "空间":
						kb_kj = f;
						break;
					case "配置":
						kb_pz = f;
						break;
					case "动力":
						kb_dl = f;
						break;
					case "操控":
						kb_ck = f;
						break;
					case "油耗":
						kb_yh = f;
						break;
					case "舒适":
						kb_ss = f;
						break;

					default:
						break;
					}
				}

				int sum = kb_wg + kb_ns + kb_kj + kb_pz + kb_dl + kb_ck + kb_yh + kb_ss;
				kb_zhpf = ((double) sum) / 8.0;
				Element yh = right.select("div.corners a:contains(有用)").get(0);
				Element hf = right.select("div.corners a:contains(回复)").get(0);
				kb_djs = Integer.parseInt(Util.getMatched(yh.text(), "(\\()(.{1,})(\\))").group(2));
				kb_hfs = Integer.parseInt(Util.getMatched(hf.text(), "(\\()(.{1,})(\\))").group(2));
				KBOfPCAuto kbOfPCAuto = new KBOfPCAuto(kb_id, kb_sfjh, kb_sftj, kb_ppid, kb_cxid, kb_cxmc, kb_fbrq,
						kb_gmsj, kb_gmdd, kb_lcj, kb_pjyh, kb_xslc, kb_nr, kb_zhpf, kb_wg, kb_ns, kb_kj, kb_pz, kb_dl,
						kb_ck, kb_yh, kb_ss, kb_hfs, kb_djs, kb_yhid, kb_yhmc);
				kbDaoOfPCAuto.insertKB(kbOfPCAuto);
			}

		}
		kbDaoOfPCAuto.updateBBSTime("pcauto_kbgxsj", kbid);
	}

}
